package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.DateUtil;
import com.puboot.common.util.Pagination;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.BizCourse;
import com.puboot.module.admin.model.BizCoursePlan;
import com.puboot.module.admin.service.BizCoursePlanService;
import com.puboot.module.admin.service.BizCourseService;
import com.puboot.module.admin.vo.CoursePlanConditionVo;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 课程计划管理
 *
 * @author ltc
 * @version 1.0
 * @date 2020/9/28 22:06
 */
@Controller
@RequestMapping("coursePlan")
@AllArgsConstructor
public class BizCoursePlanController {
    private final BizCoursePlanService bIzCoursePlanService;
    private final BizCourseService bizCourseService;


    @GetMapping("/add")
    public String add() {
        return CoreConst.ADMIN_PREFIX + "coursePlan/form";
    }




    /**
     * 查询所有课程
     * @param bizCourse
     * @return
     */
    @PostMapping("listCourse")
    @ResponseBody
    public List<BizCourse> loadcourses(BizCourse bizCourse) {
        List<BizCourse> bizCourses = bizCourseService.selectCourses(bizCourse);
        return bizCourses;
    }

    @PostMapping("list")
    @ResponseBody
    public PageResultVo loadCoursePlans(CoursePlanConditionVo vo, Integer pageNumber, Integer pageSize) {
        IPage<BizCoursePlan> page = new Pagination<>(pageNumber, pageSize);
        List<BizCoursePlan> coursePlanList = bIzCoursePlanService.PageListCoursePlans(page, vo);
        return ResultUtil.table(coursePlanList, page.getTotal());
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(BizCoursePlan bIzCoursePlan) {

        Date date = new Date();
        bIzCoursePlan.setCreateTime(date);
        bIzCoursePlan.setUpdateTime(date);
        Date startTime = bIzCoursePlan.getStartTime();
        Date endTime = bIzCoursePlan.getEndTime();
        if (null !=startTime && null !=endTime){
            String startTimeEndTime=DateUtil.getSmsDate(startTime)+"至"+DateUtil.getSmsDate(endTime);
            bIzCoursePlan.setStartEndTime(startTimeEndTime);
        }
        boolean i = bIzCoursePlanService.save(bIzCoursePlan);
        if (i)
            return ResultUtil.success("新增课程计划成功");
        return ResultUtil.error("新增课程计划失败");
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer id) {
        BizCoursePlan bIzCoursePlan = bIzCoursePlanService.getById(id);
        BizCourse bizCourse = bizCourseService.getById(bIzCoursePlan.getCourseId());
        bIzCoursePlan.setCourseName(bizCourse.getCourseName());
        bIzCoursePlan.setRemarks(bizCourse.getRemarks());
        model.addAttribute("coursePlan", bIzCoursePlan);
        return CoreConst.ADMIN_PREFIX + "coursePlan/form";
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo edit(BizCoursePlan bIzCoursePlan) {
        bIzCoursePlan.setUpdateTime(new Date());
        boolean i = bIzCoursePlanService.updateById(bIzCoursePlan);
        if (i)
            return ResultUtil.success("编辑课程计划成功");
        return ResultUtil.error("编辑课程计划失败");

    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        boolean i = bIzCoursePlanService.removeById(id);
        if (i)
            return ResultUtil.success("删除课程计划成功");
        return ResultUtil.error("删除课程计划失败");

    }

    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = bIzCoursePlanService.deleteBatch(ids);
        if (i > 0)
            return ResultUtil.success("删除课程计划成功");
        return ResultUtil.error("删除课程计划失败");
    }

}
